<template>
<div>
  <template v-if="role=='dortor'">
    <el-form ref="doctorform" :model="user" :rules="doctorrules" label-width="80px">
      <el-form-item label="医生编号" prop="number">
        <el-input v-model="user.number" />
      </el-form-item>
      <el-form-item label="医生职称" prop="title">
        <el-input v-model="user.title" maxlength="11" />
      </el-form-item>
    </el-form>
  </template>
  <template v-else>
    <el-form ref="patientform" :model="user" :rules="patientrules" label-width="80px">
      <el-form-item label="患者年龄" prop="age">
        <el-input v-model="user.age" maxlength="3"/>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="user.idCard" maxlength="18" />
      </el-form-item>
      <el-form-item label="就诊卡号" prop="medicalNumber">
        <el-input v-model="user.medicalNumber" />
      </el-form-item>
      <el-form-item label="社保卡号" prop="socialNumber">
        <el-input v-model="user.socialNumber"/>
      </el-form-item>
    </el-form>
  </template>
    <el-button type="primary" size="mini" @click="submit" class="btn">保存</el-button>
    <el-button type="danger" size="mini" @click="close">关闭</el-button>
</div>

</template>

<script>
import { updateUserProfile } from "@/api/system/user";
import { getDoctorInfo, getPatientInfo, doctorUpdate, patientUpdate } from "@/api/system/user";

export default {
  props: {
    role:{
      type: Array
    }
  },

  data() {
    return {
      user:{},
      // 医生表单校验
      doctorrules: {
        number: [
          { required: true, message: "医生编号不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "医生职称不能为空", trigger: "blur" },
        ]
      },
      // 患者表单验证
      patientrules: {
        age: [
          { required: true, message: "患者年龄不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
          { required: true, pattern: /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/, message:"请输入正确格式的身份证号码", trigger: "blur"}
        ],
        medicalNumber: [
          { required: true, message: "就诊卡号不能为空", trigger: "blur" }
        ],
        socialNumber: [
          { required: true, message: "社保卡号不能为空", trigger: "blur" },
        ]
      }
    };
  },
  mounted(){
    this.getUserInfos(this.$store.getters.userid)
  },
  methods: {
    // 获取医生患者信息
    getUserInfos(userId){
      if(this.$store.getters.roles.includes('dortor')){  // 判断医生
        getDoctorInfo({id: userId}).then(res => {
          if(res.code == 0){
            this.user = res.doctor
          }
        })
      }else if(this.$store.getters.roles.includes('patient')){ //判断患者
        getPatientInfo({id: userId}).then(res => {
          if(res.code == 0){
            this.user = res.patient
          }
        })
      }
    },
    submit() {
      if(this.role.includes('dortor')){
        this.$refs["doctorform"].validate(valid => {
          if (valid) {
            let params = {
              id:this.$store.getters.userid,
              number:this.user.number,
              title:this.user.title
            }
            doctorUpdate(params).then(response => {
              if (response.code === 0) {
                this.msgSuccess("修改成功");
              }
            });
          }
        });
      }else if(this.role.includes('patient')){
        this.$refs["patientform"].validate(valid => {
          if (valid) {
            let params = {
              id:this.$store.getters.userid,
              age:this.user.age,
              idCard:this.user.idCard,
              medicalNumber:this.user.medicalNumber,
              socialNumber:this.user.socialNumber
            }
            patientUpdate(params).then(response => {
              if (response.code === 0) {
                this.msgSuccess("修改成功");
              }
            });
          }
        });
      }

    },
    close() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: "/index" });
    }
  }
};
</script>
<style lang="scss" scoped>
.btn{
  margin-left: 80px;
}
</style>
